package com.sftz.framework.mvc.processor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.impl.processor.AbstractProcessor;

import com.sftz.modules.common.dto.WxMinappUserDto;
import com.sftz.modules.minapp.user.cache.RedissionUserCache;
import com.uxuexi.core.common.util.Util;

/**
 * 用户行为处理器，记录用户的行为
 */
public class UserBehaviorProcessor extends AbstractProcessor {

	final Log log = Logs.get();

	private static final int INVALID_ID = -1;

	@Override
	public void process(ActionContext ac) throws Throwable {

		//将用户id写到头信息里，便于nginx日志记录用户id
		Integer userId = INVALID_ID;
		HttpServletRequest request = ac.getRequest();
		String token = request.getParameter("token");

		if (!Util.isEmpty(token)) {
			WxMinappUserDto user = RedissionUserCache.getUser(token);

			if (!Util.isEmpty(user)) {
				userId = user.getId();
				if (!Util.isEmpty(userId)) {
					userId = INVALID_ID;
				}
			}
		}
		HttpServletResponse response = ac.getResponse();
		response.setHeader("X-UID", userId + "");

		doNext(ac);
		return;
	}

}
